﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Common.Interfaces.Repositories;
using WebSite.Common.Models.ViewModels;
using WebSite.DAL.Db.Repositories;
using WebSite.Models;
using WebSite.Repositories;

namespace WebSite.Controllers
{
    public class TrainingController : HWBaseController
    {
        private IPageDataRepository _repository;
        private ICourseRepository   _courseRepository;

        public TrainingController(ICourseRepository courseRepo, IPageDataRepository repo)
        {
            _courseRepository = courseRepo;
            _repository       = repo;
        }

        public TrainingController()
            : this(new DbCourseRepository("DbСontext"),
                    new PageDataDbRepository(
                        new DbMainPageItemsRepository("DbСontext"), 
                        new DbCourseGroupRepository("DbСontext"), 
                        new DbLayoutRepository("DbСontext")))
        {
        }

        //
        // GET: /Training/
        [OutputCache(CacheProfile = "StaticPage")]
        public ActionResult Course(String name)
        {
            if (String.IsNullOrEmpty(name)) 
            {
                throw new HttpException(404, "not found");
            }
            
            CourseVM course = _courseRepository.LoadByPath(name);

            if (course == null)
            {
                throw new HttpException(404, "not found");
            }

            if (course.Info == null)
                course.Info = new EditCourseInfoVM();

            return View(course);
        }

        [OutputCache(CacheProfile = "StaticPage")]
        public ActionResult Index()
        {
            TrainingPageVM pageData = _repository.LoadTrainingPageData();

            return View(pageData);
        }

        [OutputCache(CacheProfile = "StaticPage")]
        public ActionResult Category(String name)
        {
            TrainingPageVM pageData = _repository.LoadTrainingPageData();

            if (!pageData.ContainsCategory(name))
            {
                throw new HttpException(404, "not found");
            }

            pageData.SetSelection(name);

            //if (!String.IsNullOrEmpty(name))
            //{
            //    return View(name, pageData);
            //}
            //else
            //{
                return View(pageData);
            //}
        }
    }
}
